package com.zhku.shoppingspringboot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhku.shoppingspringboot.common.BaseContext;
import com.zhku.shoppingspringboot.common.CustomException;
import com.zhku.shoppingspringboot.mapper.AddressMapper;
import com.zhku.shoppingspringboot.mapper.OrderSkusMapper;
import com.zhku.shoppingspringboot.pojo.dto.AddAddressDTO;
import com.zhku.shoppingspringboot.pojo.entity.Address;
import com.zhku.shoppingspringboot.pojo.entity.OrderSkus;
import com.zhku.shoppingspringboot.service.IAddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.function.LongFunction;

/**
 * @author hwy
 * @date 2025-01-02 23:22
 */
@Service
public class AddressServiceImpl  extends ServiceImpl<AddressMapper, Address> implements IAddressService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long addAddress(AddAddressDTO addAddressDTO) {
        Address address = BeanUtil.copyProperties(addAddressDTO, Address.class);
        address.setUserId(BaseContext.getCurrentId());
        if(!save(address)){
            throw new CustomException("插入数据失败");
        }
        return address.getId();
    }

    @Override
    public List<Address> getAddress() {
        return this.lambdaQuery().eq(Address::getUserId, BaseContext.getCurrentId()).list();

    }

    @Override
    public Long modifyAddress(Address address) {
        address.setUserId(BaseContext.getCurrentId());
        if(!this.lambdaUpdate().update(address)){
            throw new CustomException("操作失败");
        }
        return address.getId();
    }

    @Override
    public Long deleteAddress(Long id) {
       if(!removeById(id)){
           throw new CustomException("删除失败");
       }
       return id;
    }
}
